001 /** 002 * Created by IntelliJ IDEA. 003 * User: Wei Wang 004 * Date: Apr 27, 2003 005 * Time: 5:48:17 PM 006 */ 007 008 package EVolve.util.phasedetectors.phasedetectorUI; 009 010 import EVolve.util.phasedetectors.*; 011 import EVolve.Scene; 012 import EVolve.visualization.*; 013 import EVolve.visualization.XYViz.XYVisualization; 014 import javax.swing.*; 015 import java.awt.event.*; 016 import java.awt.*; 017 import java.util.*; 018 019 public class PhaseDetectorToolBar extends JToolBar{ 020 public static int NumberOfSharedOptions = 3; 021 private JButton buttonRefresh, buttonClose, buttonUndo; 022 private JButton buttonAddPhase, buttonRemovePhase; 023 private JButton buttonCopyPhase, buttonPastePhase; 024 private JComboBox comboOptions; 025 026 private JButton buttonTrigger; 027 private JLabel labelNoise; 028 private JTextField noiseTolerance; 029 private int noiseToler = 2; 030 031 private HashMap controlsMap; 032 private PhaseDetector detector; 033 private String detectorName, addRemover, entityTrigger; 034 private Component commonControls[]; 035 036 037 public PhaseDetectorToolBar() { 038 super("Phase detector setting"); 039 controlsMap = new HashMap(); 040 addRemover = "Phase Add/Remove"; 041 entityTrigger = "Phase Entity Trigger"; 042 detector = null; 043 setPreferredSize(new java.awt.Dimension(800,28)); 044 FlowLayout layout = new FlowLayout(FlowLayout.LEFT,2,-2); 045 layout.preferredLayoutSize(this); 046 setLayout(layout); 047 commonControls = createCommandButtons(); 048 createPhaseDetectorToolBar(); 049 layout.layoutContainer(this); 050 } 051 052 private void createPhaseDetectorToolBar() { 053 setRollover(true); 054 055 comboOptions = new JComboBox(); 056 057 controlsMap.put(addRemover,createAddRemoverButtons()); 058 controlsMap.put(entityTrigger, createEntityTriggerButtons()); 059 comboOptions.addItem(addRemover); 060 comboOptions.addItem(entityTrigger); 061 062 comboOptions.addActionListener(new ActionListener() { 063 public void actionPerformed(ActionEvent e) { 064 String item = (String)comboOptions.getSelectedItem(); 065 int number = getComponents().length; 066 for (int i=number-1; i>=2; i--) { 067 remove(i); 068 } 069 Component[] controls = (Component[])controlsMap.get(item); 070 for (int i=0; i<controls.length; i++) { 071 add(controls[i]); 072 } 073 for (int i=0; i<commonControls.length; i++) { 074 add(commonControls[i]); 075 } 076 } 077 }); 078 comboOptions.setPreferredSize(new java.awt.Dimension(180,26)); 079 add(comboOptions); 080 addSeparator(); 081 comboOptions.setSelectedIndex(0); 082 } 083 084 public void refresh() { 085 ArrayList vizList = Scene.getVisualizationManager().getVisualizationList(); 086 for (int i=0; i<vizList.size(); i++) { 087 Visualization visual = (Visualization)vizList.get(i); 088 if (visual.isFreezed()) return; 089 } 090 091 if (detector != null) 092 updateToolBarState(detector.getToolBarState()); 093 094 Visualization visual = Scene.getVisualizationManager().getActiveVisualization(); 095 if ((visual == null) || (!(visual instanceof XYVisualization))) { 096 detector = null; 097 } else 098 detector = ((XYVisualization)visual).getPhaseDetector(); 099 100 controlsMap.remove(detectorName); 101 102 comboOptions.removeAllItems(); 103 if (detector != null) { 104 detectorName = detector.getName(); 105 controlsMap.put(detectorName,detector.createDetectorParamsControls()); 106 comboOptions.addItem(detectorName); 107 } else { 108 detectorName = ""; 109 } 110 111 comboOptions.addItem(addRemover); 112 comboOptions.addItem(entityTrigger); 113 114 if (detector != null) { //restore button/controls state 115 PhaseDetectorToolBarState state = detector.getToolBarState(); 116 comboOptions.setSelectedIndex(state.selectedOption); 117 HashMap optionControlsMap = state.optionsControlState; 118 Iterator it = controlsMap.keySet().iterator(); 119 while (it.hasNext()) { 120 String key = (String)it.next(); 121 if ((key.equals(detectorName)) || (!optionControlsMap.containsKey(key))) continue; 122 123 Component[] controls = (Component[])controlsMap.get(key); 124 boolean[] enableStates = (boolean[])optionControlsMap.get(key); 125 for (int i=0; i<controls.length; i++) { 126 controls[i].setEnabled(enableStates[i]); 127 } 128 } 129 } 130 131 enableButton(detector != null); 132 buttonClose.setEnabled(true); 133 } 134 135 public void enableButton(boolean flag) { 136 comboOptions.setEnabled(flag); 137 for (int i=0; i<commonControls.length-1; i++) { 138 commonControls[i].setEnabled(flag); 139 } 140 Iterator it = controlsMap.keySet().iterator(); 141 while (it.hasNext()) { 142 Component items[] = (Component[])controlsMap.get(it.next()); 143 for (int i=0; i<items.length; i++) { 144 items[i].setEnabled(flag); 145 } 146 } 147 boolean empty = Scene.getVisualizationManager().getPhaseOperation().clipboardIsEmpty(); 148 if (buttonPastePhase.isEnabled()) { 149 buttonPastePhase.setEnabled(!empty); 150 } 151 boolean undoable = Scene.getVisualizationManager().getPhaseOperation().undoable(); 152 buttonUndo.setEnabled(undoable); 153 } 154 155 public void updateVisualization(boolean cleanup) { 156 Visualization visual = Scene.getVisualizationManager().getActiveVisualization(); 157 if ((visual == null) || (!(visual instanceof XYVisualization))) 158 return ; 159 160 AxesPanel canvas = (AxesPanel)((JScrollPane)visual.getPanel()).getViewport().getView(); 161 PhaseDetector phaseDetector = ((XYVisualization)visual).getPhaseDetector(); 162 163 if (phaseDetector == null) return; 164 165 phaseDetector.saveSetting(); 166 if (cleanup) { 167 phaseDetector.reset(); 168 canvas.setPhases(null); 169 } else 170 phaseDetector.drawPhase(canvas); 171 canvas.repaint(); 172 } 173 174 public void updateToolBarState(PhaseDetectorToolBarState state) { 175 state.selectedOption = comboOptions.getSelectedIndex(); 176 HashMap optionControlsMap = state.optionsControlState; 177 Iterator it = controlsMap.keySet().iterator(); 178 while (it.hasNext()) { 179 String key = (String)it.next(); 180 if (key.equals(detectorName)) continue; 181 182 Component[] controls = (Component[])controlsMap.get(key); 183 boolean[] enableStates = null; 184 if (optionControlsMap.containsKey(key)) { 185 enableStates = (boolean[])optionControlsMap.get(key); 186 } else { 187 enableStates = new boolean[controls.length]; 188 optionControlsMap.put(key,enableStates); 189 } 190 for (int i=0; i<controls.length; i++) { 191 enableStates[i] = controls[i].isEnabled(); 192 } 193 } 194 } 195 196 private Component[] createEntityTriggerButtons() { 197 Component[] returnVal = new Component[3]; 198 199 buttonTrigger = new JButton(new ImageIcon(Scene.getGifURL("trigger.gif"))); 200 buttonTrigger.setToolTipText("Trigger phases with selected entities"); 201 buttonTrigger.addActionListener(new ActionListener() { 202 public void actionPerformed(ActionEvent e) { 203 try { 204 noiseToler = Integer.parseInt(noiseTolerance.getText()); 205 Scene.getVisualizationManager().getPhaseOperation().triggerPhases(noiseToler); 206 } catch (NumberFormatException exp) { 207 Scene.showErrorMessage("Noise Tolerance must be an integer!"); 208 } 209 } 210 }); 211 buttonTrigger.setPreferredSize(new java.awt.Dimension(28,28)); 212 213 labelNoise = new JLabel("Noise tolerance (intervals):"); 214 noiseTolerance = new JTextField(String.valueOf(noiseToler)); 215 noiseTolerance.setColumns(2); 216 217 218 returnVal[0] = labelNoise; 219 returnVal[1] = noiseTolerance; 220 returnVal[2] = buttonTrigger; 221 return returnVal; 222 } 223 224 private Component[] createAddRemoverButtons() { 225 Component returnVal[] = new Component[2]; 226 227 buttonAddPhase = new JButton(new ImageIcon(Scene.getGifURL("add.gif"))); 228 buttonAddPhase.setToolTipText("Add phase manually"); 229 buttonAddPhase.addActionListener(new ActionListener() { 230 public void actionPerformed(ActionEvent e) { 231 Scene.getVisualizationManager().getPhaseOperation().add(); 232 } 233 }); 234 buttonAddPhase.setPreferredSize(new java.awt.Dimension(28,28)); 235 236 buttonRemovePhase = new JButton(new ImageIcon(Scene.getGifURL("remove.gif"))); 237 buttonRemovePhase.setToolTipText("Remove phase manually"); 238 buttonRemovePhase.addActionListener(new ActionListener() { 239 public void actionPerformed(ActionEvent e) { 240 Scene.getVisualizationManager().getPhaseOperation().remove(); 241 } 242 }); 243 buttonRemovePhase.setPreferredSize(new java.awt.Dimension(28,28)); 244 245 returnVal[0] = buttonAddPhase; 246 returnVal[1] = buttonRemovePhase; 247 248 return returnVal; 249 } 250 251 private Component[] createCommandButtons() { 252 Component returnVal[] = new Component[5]; 253 254 buttonClose = new JButton(new ImageIcon(Scene.getGifURL("close.gif"))); 255 buttonClose.setToolTipText("Disable Phase Detector"); 256 buttonClose.addActionListener(new ActionListener() { 257 public void actionPerformed(ActionEvent e) { 258 Scene.getVisualizationManager().enablePhaseDetector(false); 259 setVisible(false); 260 updateVisualization(true); 261 Scene.getUIManager().refreshToolBar(); 262 } 263 }); 264 buttonClose.setPreferredSize(new java.awt.Dimension(28,28)); 265 266 buttonRefresh = new JButton(new ImageIcon(Scene.getGifURL("refresh.gif"))); 267 buttonRefresh.setToolTipText("Refresh Phase Information"); 268 buttonRefresh.addActionListener(new ActionListener() { 269 public void actionPerformed(ActionEvent e) { 270 buttonUndo.setEnabled(Scene.getVisualizationManager().getPhaseOperation().undoable()); 271 updateVisualization(false); 272 } 273 }); 274 buttonRefresh.setPreferredSize(new java.awt.Dimension(28,28)); 275 276 buttonCopyPhase = new JButton(new ImageIcon(Scene.getGifURL("copy.gif"))); 277 buttonCopyPhase.setToolTipText("Copy phase in the current visualization"); 278 buttonCopyPhase.addActionListener(new ActionListener() { 279 public void actionPerformed(ActionEvent e) { 280 Scene.getVisualizationManager().getPhaseOperation().copyPhase(); 281 boolean empty = Scene.getVisualizationManager().getPhaseOperation().clipboardIsEmpty(); 282 buttonPastePhase.setEnabled(!empty); 283 } 284 }); 285 buttonCopyPhase.setPreferredSize(new java.awt.Dimension(28,28)); 286 287 buttonPastePhase = new JButton(new ImageIcon(Scene.getGifURL("paste.gif"))); 288 buttonPastePhase.setToolTipText("Paste phase to the current visualization"); 289 buttonPastePhase.addActionListener(new ActionListener() { 290 public void actionPerformed(ActionEvent e) { 291 Scene.getVisualizationManager().getPhaseOperation().pastePhase(); 292 } 293 }); 294 buttonPastePhase.setPreferredSize(new java.awt.Dimension(28,28)); 295 buttonPastePhase.setEnabled(false); 296 297 buttonUndo = new JButton(new ImageIcon(Scene.getGifURL("undo.gif"))); 298 buttonUndo.setToolTipText("Undo"); 299 buttonUndo.addActionListener(new ActionListener() { 300 public void actionPerformed(ActionEvent e) { 301 Scene.getVisualizationManager().getPhaseOperation().undo(); 302 buttonUndo.setEnabled(Scene.getVisualizationManager().getPhaseOperation().undoable()); 303 } 304 }); 305 buttonUndo.setPreferredSize(new java.awt.Dimension(28,28)); 306 buttonUndo.setEnabled(false); 307 308 returnVal[0] = buttonCopyPhase; 309 returnVal[1] = buttonPastePhase; 310 returnVal[2] = buttonUndo; 311 returnVal[3] = buttonRefresh; 312 returnVal[4] = buttonClose; 313 314 return returnVal; 315 } 316 317 }